package com.clls.usblistenerdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * 监听usb插拔测试
 */
public class MainActivity extends AppCompatActivity {
    String TAG = "USBDEMO";
    USBBroadcastReceiver usbBroadcastReceiver;
    private TextView tvInfo;
    StringBuilder stringBuilder = new StringBuilder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvInfo = findViewById(R.id.tv_info);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        intentFilter.addAction("android.hardware.usb.action.USB_STATE");

        usbBroadcastReceiver = new USBBroadcastReceiver();
        registerReceiver(usbBroadcastReceiver, intentFilter);
        detectUsbDeviceWithUsbManager();
        AllDeviceConnected();

    }

    class USBBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
//            AllDeviceConnected();
            if (intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_ATTACHED")
                    || intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_DETACHED")) {
                UsbDevice usbDevice = (UsbDevice) intent.getExtras().get("device");
                Log.i(TAG, "onReceive: usb device status changed action ="
                        +intent.getAction()+",pid:"+usbDevice.getProductId()+",vid:"+usbDevice.getVendorId());
                stringBuilder.append("usb device status changed action ="
                        +intent.getAction()+",pid:"+usbDevice.getProductId()+",vid:"+usbDevice.getVendorId());
                stringBuilder.append("\n\t");
            }

            if (intent.getAction().equals("android.hardware.usb.action.USB_STATE")) {
                if (intent.getExtras().getBoolean("connected")) {
                    //usb 插入
                    Log.i(TAG, "onReceive: usb instert");
                    stringBuilder.append("action :android.hardware.usb.action.USB_STATE 设备插入");
                    stringBuilder.append("\n\t");

                } else {
                    Log.i(TAG, "onReceive: usb pull out");
                    stringBuilder.append("action :android.hardware.usb.action.USB_STATE 设备拔出");
                    stringBuilder.append("\n\t");
                }
            }

            tvInfo.setText(stringBuilder.toString());
        }
    }

    private void detectUsbDeviceWithUsbManager() {
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        for (Map.Entry entry : deviceList.entrySet()) {
            Log.d(TAG, "detectUsbDeviceWithUsbManager: "+entry.getKey()+","+entry.getValue());
        }
    }

    private void AllDeviceConnected() {
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        for (String key : deviceList.keySet()) {
            UsbDevice usbDevice = deviceList.get(key);
            if (usbDevice != null) {
                Log.d(TAG, "AllDeviceConnected:usbDevice.getProductId()= "
                        +usbDevice.getProductId()+",usbDevice.getVendorId()="+usbDevice.getVendorId());
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (usbBroadcastReceiver != null) {
            unregisterReceiver(usbBroadcastReceiver);
        }
    }
}